package com.qidian.service.impl;

import com.qidian.mapper.StudentInfoMapper;
import com.qidian.pojo.QdClass;
import com.qidian.mapper.QdClassMapper;
import com.qidian.pojo.StudentInfo;
import com.qidian.service.QdClassService;
import com.qidian.util.LayuiDataList;
import com.qidian.util.QdFormatDate;
import com.qidian.util.QidianResponse;
import com.qidian.vo.QdClassVO;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import sun.text.resources.FormatData;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * (QdClass表)服务实现类
 *
 * @author 戴着假发的程序员
 * @since 2022-03-01 08:47:07
 */
@Service("qdClassService")
public class QdClassServiceImpl implements QdClassService {
    @Resource
    private QdClassMapper qdClassMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;

    /**
     *  导出班级信息
     * @param tempPath  临时文件路径
     * @return ｛code:200,msg:"文件名稱",data:null｝
     */
    public QidianResponse exportClassInfo(String tempPath,Long classId){
        QidianResponse response = new QidianResponse();
        response.setCode(500);
        // 查询数据
        Map map = qdClassMapper.selectClassInfoById(classId);
        List<StudentInfo> stInfos = studentInfoMapper.queryByClassId(classId);
        //1.在内存中创建一个excel文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //2.创建工作簿
        HSSFSheet sheet = hssfWorkbook.createSheet(map.get("class_name")+"-班级信息");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("班级编号");
        row.createCell(1).setCellValue(map.get("class_id")+"");
        row = sheet.createRow(1);
        row.createCell(0).setCellValue("班级名称");
        row.createCell(1).setCellValue(map.get("class_name")!=null?map.get("class_name").toString():"");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("教学方向");
        int direction = new Integer(map.get("direction").toString());
        row.createCell(1).setCellValue(direction==0?"未定":direction==1?"java":direction==2?"前端":direction==3?"大数据":direction==4?"人工智能":direction==5?"软件测试":"");
        row = sheet.createRow(3);
        row.createCell(0).setCellValue("教员");
        row.createCell(1).setCellValue(map.get("teacher_name")!=null?map.get("teacher_name").toString():"");
        row = sheet.createRow(4);
        row.createCell(0).setCellValue("学习顾问");
        row.createCell(1).setCellValue(map.get("class_adviser_name")!=null?map.get("class_adviser_name").toString():"");
        row = sheet.createRow(5);
        row.createCell(0).setCellValue("就业顾问");
        row.createCell(1).setCellValue(map.get("employment_adviser_name")!=null?map.get("employment_adviser_name").toString():"");
        row = sheet.createRow(6);
        row.createCell(0).setCellValue("开班时间");
        row.createCell(1).setCellValue(map.get("start_time")!=null ? map.get("start_time").toString():"");//.toString():"");
        row = sheet.createRow(7);
        row.createCell(0).setCellValue("结班时间");
        row.createCell(1).setCellValue(map.get("end_time")!=null?map.get("end_time").toString():"");
        row = sheet.createRow(8);
        row.createCell(0).setCellValue("创建时间");
        row.createCell(1).setCellValue(map.get("create_time")!=null?map.get("create_time").toString():"");
        row = sheet.createRow(9);
        row.createCell(0).setCellValue("状态");
        int status = new Integer(map.get("class_status").toString());
        row.createCell(1).setCellValue(status==1?"正常":status==2?"结业":status==3?"就业":status==4?"毕业":"删除");

        row =sheet.createRow(11);
        row.createCell(0).setCellValue("学员编号");
        row.createCell(1).setCellValue("学员姓名");
        row.createCell(2).setCellValue("登陆账号");
        row.createCell(3).setCellValue("生日");
        row.createCell(4).setCellValue("电话");
        row.createCell(5).setCellValue("微信");
        row.createCell(6).setCellValue("QQ");
        row.createCell(7).setCellValue("毕业院校");
        row.createCell(8).setCellValue("所学专业");
        row.createCell(9).setCellValue("毕业时间");
        row.createCell(10).setCellValue("专业技术掌握情况");
        row.createCell(11).setCellValue("是否住宿");
        row.createCell(12).setCellValue("宿舍号");
        row.createCell(13).setCellValue("报到时间");
        row.createCell(14).setCellValue("是否提供电脑");
        row.createCell(15).setCellValue("咨询师");
        row.createCell(16).setCellValue("学生来源说明");
        row.createCell(17).setCellValue("人脉联系人");
        row.createCell(18).setCellValue("学习动机");
        row.createCell(19).setCellValue("学费来源");
        row.createCell(20).setCellValue("身份证号码");
        row.createCell(21).setCellValue("备注");
        for(int i = 0;i <stInfos.size();i++){
            StudentInfo info = stInfos.get(i);
            row = sheet.createRow(11+i+1);
            row.createCell(0).setCellValue(info.getStudentId());
            row.createCell(1).setCellValue(info.getStudentName());
            row.createCell(2).setCellValue(info.getAccommodationNo());
            row.createCell(3).setCellValue(QdFormatDate.formatDate(info.getStudentBirth()));
            row.createCell(4).setCellValue(info.getStudentTel());
            row.createCell(5).setCellValue(info.getStudentWechat());
            row.createCell(6).setCellValue(info.getStudentQq());
            row.createCell(7).setCellValue(info.getStudentUniversity());
            row.createCell(8).setCellValue(info.getStudentMajor());
            row.createCell(9).setCellValue(info.getStudentGraduationTiem());
            row.createCell(10).setCellValue(info.getStudentTechnology());
            row.createCell(11).setCellValue(info.getStudentIsAccommodation()==1?"是":"否");
            row.createCell(12).setCellValue(info.getAccommodationNo());
            row.createCell(13).setCellValue(info.getAdmissionDate());
            row.createCell(14).setCellValue(info.getProvidedComputer()==1?"是":"否");
            row.createCell(15).setCellValue(info.getConsultant());
            row.createCell(16).setCellValue(info.getSourceDescription());
            row.createCell(17).setCellValue(info.getLinkMan());
            row.createCell(18).setCellValue(info.getStudyMotivation());
            row.createCell(19).setCellValue(info.getCostSource());
            row.createCell(20).setCellValue(info.getIdcard());
            row.createCell(21).setCellValue(info.getRemark());
        }

        String fileName = UUID.randomUUID().toString().replace("-","")+".xls";
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(tempPath+"/"+fileName);
            hssfWorkbook.write(out);


        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("导出文件"+fileName+"，生成临时文件失败:"+e.getMessage());
        }finally{
            try{
                if(hssfWorkbook!=null){
                    hssfWorkbook.close();
                }
                if(out != null )
                    out.close();;
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
        response.setCode(200);
        response.setMsg(fileName);
        return  response;
    };

    /**
     * 通过ID查询单条数据
     *
     * @param classId 主键
     * @return 实例对象
     */
    @Override
    public QidianResponse selectById(Long classId) {
        QidianResponse response = new QidianResponse();
        response.setCode(200);
        response.setData(qdClassMapper.selectById(classId));
        return response;
    }

    /**
     * 分页查询
     *
     * @param page 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<QdClass> selectPage(int page, int limit) {
        int start = (page-1)*limit;
        return this.qdClassMapper.selectPage(start, limit);
    }
    /**
     * 分页查询
     *
     * @param page 查询起始位置
     * @param limit 查询条数
     * @param qdClass 实例对象
     * @return 对象列表
     */
    public List<QdClass> selectPage(int page, int limit,QdClass qdClass){
        int start = (page-1)*limit;
        return this.qdClassMapper.selectPageByQdClass(start, limit,null);
    }
    /**
     * 查询所有
     *
     * @return 实例对象的集合
     */
     @Override
     public List<QdClass> selectAll() {
        return this.qdClassMapper.selectAll();
     }
     
    /**
     * 根据条件查询
     *
     * @return 实例对象的集合
     */
    @Override
    public List<QdClass> selectList(QdClass qdClass) {
        return this.qdClassMapper.selectList(qdClass);
    }
    
    /**
     * 新增数据
     *
     * @param qdClass 实例对象
     * @return QidianResponse
     */
    @Override
    public QidianResponse insert(QdClass qdClass) {
        QidianResponse response = new QidianResponse();
        response.setCode(500);
        // 查重
        if(qdClassMapper.selectByName(qdClass.getClassName())!=null){
            response.setCode(501);
            return response;
        }
        // 补全数据
        qdClass.setCreateTime(new Date());
        qdClass.setModifyTime(new Date());
        qdClass.setClassStatus(1);
        if(qdClassMapper.insert(qdClass)==1)
            response.setCode(200);
        return response;
    }

    /**
     * 批量新增
     *
     * @param qdClasss 实例对象的集合
     * @return 生效的条数
     */
    @Override
    public int batchInsert(List<QdClass> qdClasss) {
        return this.qdClassMapper.batchInsert(qdClasss);
    }

    /**
     * 修改数据
     *
     * @param qdClass 实例对象
     * @return 实例对象
     */
    @Override
    public QidianResponse update(QdClass qdClass) {
        QidianResponse response = new QidianResponse();
        response.setCode(500);
        // 查重
        QdClass clzz = qdClassMapper.selectByName(qdClass.getClassName());
        if(clzz != null && clzz.getClassId() != qdClass.getClassId()){
            response.setCode(501);
            return response;
        }
        if(this.qdClassMapper.update(qdClass)==1)
            response.setCode(200);
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param classId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Long classId) {
        return this.qdClassMapper.deleteById(classId);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public int deleteByIds(Long [] ids) {
        return this.qdClassMapper.deleteByIds(ids);
    }
    
    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
     @Override
     public int count(){
        return this.qdClassMapper.count();
     }

     /**
     * 查询总数据数
     *
     * @return 数据总数
     */
    public int countByQdClass(QdClass qdClass){
        return this.qdClassMapper.countByQdClass(null);
    }

                /**
     * 分页查询1
     *
     * @param page 查询起始位置
     * @param limit 查询条数
     * @param vo 实例对象
     * @return Map
     */
    public LayuiDataList<QdClass> selectPageForLayui(int page, int limit, QdClassVO vo){
        int start = (page-1)*limit;
        LayuiDataList<QdClass> dataList = new LayuiDataList<>();
        dataList.setCode(0);
        dataList.setMsg("");
        dataList.setCount(this.qdClassMapper.countByQdClass(vo));
        dataList.setData(this.qdClassMapper.selectPageByQdClass(start, limit,vo));
        return dataList;
    };
}